/*
 * Copyright (C) 2019 ~ 2022 Uniontech Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "exefilespolicykithelper.h"
#include "core/polkitqt1-authority.h"

#include <unistd.h>

ExeFilesPolicyKitHelper::ExeFilesPolicyKitHelper()
{
}

ExeFilesPolicyKitHelper::~ExeFilesPolicyKitHelper()
{
}

void ExeFilesPolicyKitHelper::confirmRemoveExeFiles(QStringList sFiles)
{
    // 权限验证
    bool isbChange = checkAuthorization("com.deepin.deepin-defender.checkAuthentication", getpid());
    Q_EMIT notifyRemoveExeFiles(isbChange, sFiles);
}

void ExeFilesPolicyKitHelper::confirmTrustExeFiles(const QString &file)
{
    // 权限验证
    bool isbChange = checkAuthorization("com.deepin.deepin-defender.checkAuthentication", getpid());
    Q_EMIT notifyTrustExeFiles(isbChange, file);
}

void ExeFilesPolicyKitHelper::confirmModelSetting(int model)
{
    // 权限验证
    bool isbChange = checkAuthorization("com.deepin.deepin-defender.checkAuthentication", getpid());
    Q_EMIT notifyModelSetting(isbChange, model);
}

bool ExeFilesPolicyKitHelper::checkAuthorization(const QString &actionId, qint64 applicationPid)
{
    PolkitQt1::Authority::Result result;
    result = PolkitQt1::Authority::instance()->checkAuthorizationSync(actionId, PolkitQt1::UnixProcessSubject(applicationPid), /// 第一个参数是需要验证的action，和规则文件写的保持一致
                                                           PolkitQt1::Authority::AllowUserInteraction);

    // 权限校验完成
    if (result == PolkitQt1::Authority::Yes) {
        return true;
    } else {
        return false;
    }
}
